﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Foxit.PDF;
using Foxit.PDF.Imaging;
using Foxit.PDF.PageElements;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.ESG.Entities;
using Sustainalytics.Reporting.Framework;

namespace Sustainalytics.ReportGenerator.BL
{
    public class ControversyReportSections : IPdfDocumentGenerator
    {
        private CompanyReportDataOffline _dataSource;
        private PdfReport _report;
       
        private List<OutlinePosition> _outlinePositions;
        private List<PageElementPosition> _pageElementPositions;
        private byte[] _incidentsMapImage;
        private Dictionary<string, byte[]> _controversiesChartImages;

        # region Static Images

        private static byte[] _companyLogoHeaderImage;
        private static byte[] _companyLogoFooterImage;
        public byte[] CompanyLogoHeaderImage { get { return _companyLogoHeaderImage; } }
        public byte[] CompanyLogoFooterImage { get { return _companyLogoFooterImage; } }


        private static byte[] _outlookArrowDown;
        private static byte[] _outlookArrowUp;
        private static byte[] _outlookArrowRight;
        public byte[] OutlookArrowDown { get { return _outlookArrowDown; } }
        public byte[] OutlookArrowUp { get { return _outlookArrowUp; } }
        public byte[] OutlookArrowRight { get { return _outlookArrowRight; } }


        private static void LoadStaticImages()
        {
            if (_companyLogoHeaderImage == null)
                _companyLogoHeaderImage = ImagesGenerator.GetJpgImage(@"Images\sustaynalytics_logo_header.jpg");

            if (_companyLogoFooterImage == null)
                _companyLogoFooterImage = ImagesGenerator.GetJpgImage(@"Images\sustaynalytics_logo_footer.jpg");


            if (_outlookArrowDown == null)
                _outlookArrowDown = ImagesGenerator.GetJpgImage(@"Images\outlook_arrow_down.jpg");

            if (_outlookArrowUp == null)
                _outlookArrowUp = ImagesGenerator.GetJpgImage(@"Images\outlook_arrow_up.jpg");

            if (_outlookArrowRight == null)
                _outlookArrowRight = ImagesGenerator.GetJpgImage(@"Images\outlook_arrow_right.jpg");
        }

        #endregion

        static ControversyReportSections()
        {
            LoadStaticImages();
        }

        public GenerationResult GeneratePdfDocument(CompanyReportDataOffline crdo)
        {
            var result = new GenerationResult
            {
                ReportType = crdo.ReportType,
                ReportGeneratorType = ReportGeneratorType.CompanyReportSections,
            };
            var sw = Stopwatch.StartNew();

            _dataSource = crdo;
            _report = new PdfReport();

            _outlinePositions = new List<OutlinePosition>();
            _pageElementPositions = new List<PageElementPosition>();

            var imagesGenerator = new ImagesGenerator();

            var headquarterLocation = new LocationNullable
            {
                Latitude = _dataSource.CorporateData.HeadquarterLatitude,
                Longitude = _dataSource.CorporateData.HeadquarterLongitude
            };

            var incidentsLocations = GetCompanyIncidentsLocations();

            _incidentsMapImage = imagesGenerator.GetIncidentsMapImageBig(incidentsLocations, headquarterLocation);

            _controversiesChartImages = imagesGenerator.GetControversiesChartImages(_dataSource.ComparisonGroupControversies, _dataSource.ControversyIndicators);


            ComposeFirstSections();

            if (_report.Pages.Any())
                result.PdfDocument = _report.Draw();

            sw.Stop();

            result.GenerationTime = sw.Elapsed;
            result.OutlinePositions = _outlinePositions;
            result.PageElementPositions = _pageElementPositions;

            return result;
        }

        private void ComposeFirstSections()
        {
            const float headerPosition = 25f;
            const float companyInfoPosition = 75f;
            const float controversySummaryPosition = 125f;
            const float distributionOfControversyAssessmentsPosition = 550f;

            var firstPage = _report.NewPage();

            AddHeader(firstPage, headerPosition);
            AddCompanyInfo(firstPage, _report.LeftMargin, companyInfoPosition);
 
            AddControversySummarySection(firstPage, controversySummaryPosition);
            AddDistributionOfControversyAssessments(firstPage, distributionOfControversyAssessmentsPosition);

            _report.Pages.Add(firstPage);

            var secondPage = _report.NewPage();

            AddControversyIndicators(secondPage, 0.0f);

            _report.Pages.Add(secondPage);


            _outlinePositions.Add(
                new OutlinePosition { Name = "Company Information", PageNumber = 1 });

            var outline = new OutlinePosition
            {
                Name = "Controversy Summary",
                PageNumber = 1,
                ChildOutlinePositions = new List<OutlinePosition>
                {
                    new OutlinePosition { Name = "Qualitative Performance - Controversies", PageNumber = 1, VerticalPosition = controversySummaryPosition },
                    new OutlinePosition { Name = "Distribution of Controversy Assessments", PageNumber = 2, VerticalPosition = distributionOfControversyAssessmentsPosition },
                    new OutlinePosition { Name = "Controversies", PageNumber = 2, VerticalPosition = 0.0f},
                }
            };
            _outlinePositions.Add(outline);
        }

        private void AddHeader(PdfReportPage page, float top)
        {
            const string reportTitle = "CONTROVERSY REPORT";

            var left = _report.LeftMargin;

            page.Elements.Add(new Label(_dataSource.CorporateData.CompanyName, left, top - 12, 340f, 60f, Fonts.CalibriBold, 24, TextAlign.Left, RgbColor.Black));
            page.Elements.Add(new Label(reportTitle, left, top + 18, page.Report.PageMaxWidth, 15f, Fonts.CalibriBold, 12, TextAlign.Right, RgbColor.DimGray));

            page.Elements.Add(new Line(left, top + 40, 550, top + 40, 2, Colors.LineColor));
            page.Elements.Add(new Image(ImageData.GetImage(CompanyLogoHeaderImage), left + 540f, top - 20, 0.2f) { Align = Align.Right, VAlign = VAlign.Top });
        }

        private void AddCompanyInfo(PdfReportPage page, float left, float top)
        {
            var grid = new PdfGrid
            {
                Page = page,
                Left = left,
                Top = top,
                Rows = 2,
                Columns = 3,
                Height = 30f,
                ShowGridLines = false
            };

            grid.AddChildren(new PdfLineTextBlock
            {
                Row = 1,
                Column = 1,
                NameFont = Fonts.CalibriLight,
                ValueFont = Fonts.CalibriBold,
                FontSize = 10f,
                Name = "Industry:",
                Value = _dataSource.CorporateData.PeerGroup,
                LeftOffset = 50f
            });
            grid.AddChildren(new PdfLineTextBlock
            {
                Row = 2,
                Column = 1,
                NameFont = Fonts.CalibriLight,
                ValueFont = Fonts.CalibriBold,
                FontSize = 10f,
                Name = "Domicile:",
                Value = _dataSource.CorporateData.Country,
                LeftOffset = 50f
            });

            var marketCap = string.Empty;
            if (_dataSource.FinancialData.MarketCapitalization != null && _dataSource.FinancialData.MarketCapitalization > 0)
                marketCap = string.Format("{0} mm. USD", _dataSource.FinancialData.MarketCapitalization.Value.ToString("###,###,###"));

            grid.AddChildren(new PdfLineTextBlock
            {
                Row = 1,
                Column = 2,
                NameFont = Fonts.CalibriLight,
                ValueFont = Fonts.CalibriBold,
                FontSize = 10f,
                Name = "Marketcap.:",
                Value = marketCap,
                LeftOffset = 60f
            });
            grid.AddChildren(new PdfLineTextBlock
            {
                Row = 2,
                Column = 2,
                NameFont = Fonts.CalibriLight,
                ValueFont = Fonts.CalibriBold,
                FontSize = 10f,
                Name = "Ticker:",
                Value = string.Format("{0}:{1}", _dataSource.CorporateData.MainExchange, _dataSource.CorporateData.MainTicker),
                LeftOffset = 60f
            });

            grid.AddChildren(new PdfLineTextBlock
            {
                Row = 1,
                Column = 3,
                NameFont = Fonts.CalibriLight,
                ValueFont = Fonts.CalibriBold,
                FontSize = 10f,
                Name = "Updated:",
                Value = _dataSource.CorporateData.LastControversyUpdate != null ? _dataSource.CorporateData.LastControversyUpdate.Value.ToString("dd MMMM yyyy", new CultureInfo("en-US")) : "N/A",
                LeftOffset = 60f
            });
            grid.AddChildren(new PdfLineTextBlock
            {
                Row = 2,
                Column = 3,
                NameFont = Fonts.CalibriLight,
                ValueFont = Fonts.CalibriBold,
                FontSize = 10f,
                Name = "Employees:",
                Value = _dataSource.CorporateData.TotalEmployees.HasValue
                    ? _dataSource.CorporateData.TotalEmployees.Value.ToString("###,###,###")
                    : string.Empty,
                LeftOffset = 60f
            });
            grid.Draw();
        }


        private void AddControversySummarySection(PdfReportPage page, float verticalPosition)
        {
            const float sectionTitleHeight = 22f;
            const float sectionFontSize = 16f;
            const float subSectionFontSize = 14f;
            const float emptyRowHeight = 10f;

            var tableWidth = _report.PageMaxWidth;
            const float tableHeight = sectionTitleHeight + emptyRowHeight +
                sectionTitleHeight + emptyRowHeight ;

            var table = new Table(_report.LeftMargin, verticalPosition, tableWidth, tableHeight);
            table.Border.Width = 0.0f;
            table.Border.Color = Grayscale.Black;
            table.Border.LineStyle = LineStyle.None;
            table.BackgroundColor = RgbColor.White;
            table.CellSpacing = 0.0f;
            table.CellDefault.Align = TextAlign.Left;
            table.CellDefault.VAlign = VAlign.Top;
            table.CellDefault.Padding.Value = 0.0f;
            table.CellDefault.Border.Width = 0.0f;
            table.CellDefault.Border.LineStyle = LineStyle.None;
            table.CellDefault.Border.Color = RgbColor.White;


            table.Columns.Add(tableWidth);

            var row = table.Rows.Add(sectionTitleHeight);


            var cell = row.Cells.Add(
                new Label("Controversy Summary", 0.0f, 0.0f, tableWidth,
                    sectionFontSize + 2f, Fonts.CalibriBold, sectionFontSize, RgbColor.White));
            cell.Padding.Left = 6f;
            cell.Padding.Top = 3f;
            cell.Border.LineStyle = LineStyle.None;
            cell.BackgroundColor = Colors.SectionColor;


            row = table.Rows.Add(emptyRowHeight);
            row.Cells.Add(string.Empty);

            row = table.Rows.Add(sectionTitleHeight);
            cell = row.Cells.Add(
                new Label("Qualitative Performance - Controversies", 0.0f, 0.0f, tableWidth,
                    sectionFontSize + 2f, Fonts.CalibriBold, subSectionFontSize));
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1.5f;
            cell.Border.Bottom.Color = RgbColor.Black;

            row = table.Rows.Add(emptyRowHeight);
            row.Cells.Add(string.Empty);

            page.AddElement(table);

            const float mapScale = 0.38f;
            const float chartHeight = 90f;
            var greenBulletColor = new RgbColor(65, 177, 166);
            var yellowBulletColor = new RgbColor(246, 176, 30);

            var left = _report.LeftMargin;
            var chartTop = verticalPosition + table.Height;
            var mapTop = verticalPosition + table.Height + 70f;

            page.AddElement(new Image(ImageData.GetImage(_incidentsMapImage), left - 40f, mapTop, mapScale));
            page.Elements.Add(new Rectangle(left - 40f, mapTop, 610f, 300, RgbColor.White, null, 40f));
            mapTop += 200;

            page.AddElement(new Circle(left, mapTop, 4f, 4f, yellowBulletColor, yellowBulletColor, 1f, LineStyle.Solid));
            page.AddElement(new Label("Incidents", left + 10f, mapTop - 5f, 50f, 10f, Fonts.CalibriRegular, 11, TextAlign.Left, RgbColor.Black));

            mapTop += 15;

            page.AddElement(new Circle(left, mapTop, 4f, 4f, greenBulletColor, greenBulletColor, 1f, LineStyle.Solid));
            page.AddElement(new Label("Headquarters", left + 10f, mapTop - 5f, 70f, 10f, Fonts.CalibriRegular, 11, TextAlign.Left, RgbColor.Black));


            var chart = new QualitativePerformanceChart(left, chartTop, _report.PageMaxWidth, chartHeight)
            {
                QualitativePerformance = GetQualitativePerformance()
            };

            chart.PreDraw();

            chart.PageElementPositions.ForEach(pep => _pageElementPositions.Add(new PageElementPosition
            {
                Id = pep.Id,
                Name = pep.Name,
                PageNumber = 1,
                HorizontalPosition = pep.HorizontalPosition,
                VerticalPosition = pep.VerticalPosition,
                Width = pep.Width,
                Height = pep.Height
            }));

            page.AddElement(chart);
        }

        private void AddDistributionOfControversyAssessments(PdfReportPage page, float top)
        {
            var left = _report.LeftMargin;
            const float sectionTitleHeight = 22f;
            const float sectionFontSize = 16f;
            const float subSectionFontSize = 14f;
            const float emptyRowHeight = 10f;

            var tableWidth = _report.PageMaxWidth;
            const float tableHeight =   sectionTitleHeight + emptyRowHeight;

            var table = new Table(_report.LeftMargin, top, tableWidth, tableHeight);
            table.Border.Width = 0.0f;
            table.Border.Color = Grayscale.Black;
            table.Border.LineStyle = LineStyle.None;
            table.BackgroundColor = RgbColor.White;
            table.CellSpacing = 0.0f;
            table.CellDefault.Align = TextAlign.Left;
            table.CellDefault.VAlign = VAlign.Top;
            table.CellDefault.Padding.Value = 0.0f;
            table.CellDefault.Border.Width = 0.0f;
            table.CellDefault.Border.LineStyle = LineStyle.None;
            table.CellDefault.Border.Color = RgbColor.White;


            table.Columns.Add(tableWidth);

            var row = table.Rows.Add(sectionTitleHeight);
            var cell = row.Cells.Add(
                new Label("Distribution of Controversy Assessments", 0.0f, 0.0f, tableWidth,
                    sectionFontSize + 2f, Fonts.CalibriBold, subSectionFontSize));
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1.5f;
            cell.Border.Bottom.Color = RgbColor.Black;

            row = table.Rows.Add(emptyRowHeight);
            row.Cells.Add(string.Empty);

            page.AddElement(table);

           

            var controversyIndicators = GetControversyIndicators();

            if (!controversyIndicators.Any()) return;

            var count = 1;
            var leftImage = left;
            var topImage = top + tableHeight;

            foreach (var controversyIndicator in controversyIndicators.OrderBy(ci => ci.SortKey))
            {
                var chartImage = GetControversiesChartImage(controversyIndicator.Number);

                if (chartImage != null)
                    page.AddElement(new Image(ImageData.GetImage(chartImage), leftImage, topImage + 15, 0.35f));

                page.AddElement(new Label(controversyIndicator.Name, leftImage + 20, topImage, 80, 30, Fonts.CalibriLight, 7, TextAlign.Center, RgbColor.Black));

                leftImage = leftImage + 100;
                count++;

                if (count <= 5) continue;

                count = 1;
                leftImage = left;
                topImage = topImage + 90f;
            }

            var topLegend = topImage;
            const float leftLegend = 220f;

            page.Elements.Add(new Rectangle(leftLegend, topLegend + 1f, 5f, 5f, new RgbColor(249, 178, 96), new RgbColor(249, 178, 96), 0.1f));
            page.Elements.Add(new Label("Company Controversy Level", leftLegend + 10f, topLegend, 100f, 10f, Fonts.CalibriLight, 8, TextAlign.Left, RgbColor.Black));

            topLegend += 10;

            page.Elements.Add(new Rectangle(leftLegend, topLegend + 1f, 5f, 5f, new RgbColor(166, 166, 166), new RgbColor(166, 166, 166), 0.1f));
            page.AddElement(new Label("Industry Controversy Level", leftLegend + 10f, topLegend, 100f, 10f, Fonts.CalibriLight, 8, TextAlign.Left, RgbColor.Black));


        }

        private byte[] GetControversiesChartImage(string controversyIndicatorNumber)
        {
            byte[] image;

            if (_controversiesChartImages == null)
                return null;

            return _controversiesChartImages.TryGetValue(controversyIndicatorNumber, out image) ? image : null;
        }

        private List<ControversyIndicator> GetControversyIndicators()
        {
            var controversyIndicators = new List<ControversyIndicator>();

            if (!_dataSource.EventIndicators.Any()) return controversyIndicators;

            controversyIndicators = _dataSource.EventIndicators.Where(ei => ei.ProvidesControversy)
                .Select(ei => new ControversyIndicator
                {
                    Id = ei.ControversyIndicatorId,
                    Name = ei.ControversyIndicatorName,
                    Number = ei.ControversyIndicatorNumber,
                    SortKey = ei.ControversyIndicatorSortKey,
                    CompanyId = ei.CompanyId,
                    AnswerCategory = ei.AnswerCategory,
                    AnswerCategoryScore = ei.AnswerCategoryScore,
                    AnswerCategoryText = ei.AnswerCategoryText,
                    Outlook = ei.OutlookScore
                }).ToList();

            return controversyIndicators;
        }

        private List<ControversyCategory> GetQualitativePerformance()
        {
            var qualitativePerformance = new List<ControversyCategory>();

            var eventIndicators = _dataSource.EventIndicators
                                             .Where(ei => ei.AnswerCategory > 0)
                                             .ToList();

            eventIndicators.ForEach(ei => qualitativePerformance.Add(new ControversyCategory
            {
                AnswerCategory = ei.AnswerCategory,
                BaseEventIndicatorId = ei.BaseEventIndicatorId,
                BaseEventIndicatorName = ei.Name,
                ControversyIndicatorId = ei.ControversyIndicatorId,
                ControversyIndicatorName = ei.ControversyIndicatorName,
                ControversyIndicatorSortKey = ei.ControversyIndicatorSortKey
            }));

            return qualitativePerformance;
        }

        private List<Location> GetCompanyIncidentsLocations()
        {
            var companyIncidentsLocations = new List<Location>();
            
            if (_dataSource.Incidents != null && _dataSource.Incidents.Any())
            {
                companyIncidentsLocations = _dataSource.Incidents.GroupBy(i => new { i.Location.Address, i.Location.Latitude, i.Location.Longitude })
                                                      .Select(group => new Location
                                                      {
                                                          Address = group.Key.Address,
                                                          Latitude = group.Key.Latitude,
                                                          Longitude = group.Key.Longitude
                                                      })
                                                      .ToList();
            }

            return companyIncidentsLocations;
        }

        private RPControversyIndicator GetControversyIndicator(Guid controversyIndicatorId)
        {
            return _dataSource.ControversyIndicators.SingleOrDefault(ci => ci.Template.Id == controversyIndicatorId);
        }

        private void AddControversyIndicators(PdfReportPage page, float top)
        {
            var left = _report.LeftMargin;
            
            var backgroundColor = new RgbColor(188, 189, 192);
            var theme = string.Empty;
            var thinLine = false;

            const string themeFormattedText = "<p><font color='FFFFFF'><b>Controversies</b></font></p>";

            page.Elements.Add(new Rectangle(left, top, 530, 24, Colors.BarBackgroundColor, Colors.BarBackgroundColor));
            page.Elements.Add(new FormattedTextArea(themeFormattedText, left + 5f, top + 3f, 500, 16, FontFamily.Times, 16, false));
            page.Elements.Add(new Line(left, top + 30f, left + 530, top + 30, 0.5f, Colors.BarBackgroundColor));

            top = top + 40;

            if (!_dataSource.IndicatorTemplates.Any()) return;

            foreach (var templateIndicator in _dataSource.IndicatorTemplates.Where(i => i.IsControversyIndicator).OrderBy(i => i.SortKey))
            {
                if (theme != templateIndicator.ThemeName)
                {
                    page.Elements.Add(new Rectangle(left, top, 530, 24, backgroundColor, backgroundColor));
                    page.Elements.Add(new Label(string.Format("{0} - Qualitative Performance", templateIndicator.ThemeName), left + 5f, top + 2f, 500, 12, Font.TimesBold, 14));
                    top = top + 30;
                    thinLine = false;
                }
                theme = templateIndicator.ThemeName;


                var controversyIndicator = GetControversyIndicator(templateIndicator.Id);

                if (controversyIndicator == null) continue;

                if (thinLine)
                {
                    page.Elements.Add(new Line(left, top, left + 530, top, 0.5f, RgbColor.Black));
                }
                else
                    thinLine = true;

                if (controversyIndicator.AnswerCategory > 0)
                {
                    top = top + 30;

                    var indicatorNameFormattedText = string.Format("<p><b>{0}</b></p>", templateIndicator.Name);
                    page.AddElement(new FormattedTextArea(indicatorNameFormattedText, left, top, 190f, 25f, Fonts.CalibriFontFamily, 10, false));

                    AddCategoryElement(page.Elements, controversyIndicator.AnswerCategory, left + 190f, top, true);

                    float estimatedHeight = 0;

                    var eventIndicators = controversyIndicator.EventIndicators.Where(ei => ei.AnswerCategory > 0).OrderBy(ei => ei.Name).ToList();

                    if (eventIndicators.Any())
                    {
                        var eiLeft = left + 235f;
                        var eiTop = top + 5f;
                        const float eiWidth = 135f;
                        const int eiFontSize = 9;

                        eventIndicators.ForEach(ei =>
                        {
                            page.Elements.Add(
                                new Label(ei.Name, eiLeft, eiTop, eiWidth, eiFontSize + 2f, Fonts.CalibriRegular, eiFontSize));
                            
                            _pageElementPositions.Add(new PageElementPosition
                            {
                                Id = ei.Id,
                                PageNumber = 2,
                                Name = ei.Name,
                                HorizontalPosition = eiLeft,
                                VerticalPosition = eiTop,
                                Height = eiFontSize + 2f,
                                Width = eiWidth
                            });

                            eiTop += 12f;
                            estimatedHeight = estimatedHeight + 12f;
                        });

                        estimatedHeight = estimatedHeight + 10;
                    }
                    if (controversyIndicator.OutlookScore.HasValue)
                        AddOutlookElement(page.Elements, controversyIndicator.OutlookScore.Value, left + 395f, top, 1);

                    top = top + 30;

                    if (estimatedHeight > 30)
                        top = top + (estimatedHeight - 30);
                }
                else
                {
                    top = top + 15;

                    var indicatorNameFormattedText = string.Format("<p><b>{0}</b></p>", templateIndicator.Name);
                    page.Elements.Add(new FormattedTextArea(indicatorNameFormattedText, left, top, 190f, 25f, Fonts.CalibriFontFamily, 10, false));

                    page.Elements.Add(new Label("No evidence of relevant controversies", left + 190f, top, 250, 10, Fonts.CalibriBold, 10));

                    top = top + 25;
                }
            }
        }

        private void AddCategoryElement(Group pageElements, int category, float left, float top, bool hasLine)
        {
            pageElements.Add(new Circle(left + 20f, top, 20f, 20f, Colors.CtgColor[category], Colors.CtgColor[category], 0, LineStyle.Solid));
            pageElements.Add(new Label(category.ToString("0"), left, top - 16, 40f, 40f, Fonts.CalibriBold, 35, TextAlign.Center, RgbColor.White));

            if (hasLine)
            {
                string categoryFormattedText = string.Format("<p><b>Category {0} - {1}</b></p>", category, GetEventSeverity(category));
                var categoryFormattedTextArea = new FormattedTextArea(categoryFormattedText, left + 45f, top - 14f, 120f, 10f, Fonts.CalibriFontFamily, 9, false);
                pageElements.Add(categoryFormattedTextArea);

                pageElements.Add(new Line(left + 45f, top, left + 180f, top, 0.5f, RgbColor.Black));
            }
            else
            {
                pageElements.Add(new Label(string.Format("Category {0}", category), left + 50, top - 12f, 50f, 10f, Fonts.CalibriBold, 9, TextAlign.Left, RgbColor.Black));
                pageElements.Add(new Label(GetEventSeverity(category), left + 50, top + 2, 50f, 10f, Fonts.CalibriBold, 9, TextAlign.Left, RgbColor.Black));
            }
        }

        private string GetEventSeverity(int category)
        {
            switch (category)
            {
                case 1:
                    return "Low";
                case 2:
                    return "Moderate";
                case 3:
                    return "Significant";
                case 4:
                    return "High";
                case 5:
                    return "Severe";
                default:
                    return string.Empty;
            }
        }

        private void AddOutlookElement(Group pageElements, int outlook, float left, float top, int type)
        {
            if (type == 0 || type == 1)
            {
                if (outlook == 0)
                    pageElements.Add(new Image(ImageData.GetImage(OutlookArrowRight), left - 15f, top - 20f, 0.08f) { Align = Align.Left, VAlign = VAlign.Top });

                if (outlook == 1)
                    pageElements.Add(new Image(ImageData.GetImage(OutlookArrowUp), left - 15f, top - 20f, 0.08f) { Align = Align.Left, VAlign = VAlign.Top });

                if (outlook == -1)
                    pageElements.Add(new Image(ImageData.GetImage(OutlookArrowDown), left - 15f, top - 20f, 0.08f) { Align = Align.Left, VAlign = VAlign.Top });
            }
            else
            {
                if (outlook == 0)
                    pageElements.Add(new Image(ImageData.GetImage(OutlookArrowRight), left, top, 0.06f) { Align = Align.Left, VAlign = VAlign.Top });

                if (outlook == 1)
                    pageElements.Add(new Image(ImageData.GetImage(OutlookArrowUp), left, top, 0.06f) { Align = Align.Left, VAlign = VAlign.Top });

                if (outlook == -1)
                    pageElements.Add(new Image(ImageData.GetImage(OutlookArrowDown), left, top, 0.06f) { Align = Align.Left, VAlign = VAlign.Top });
            }

            if (type == 0)
            {
                pageElements.Add(new Label("Outlook", left + 35, top - 14f, 50f, 10f, Fonts.CalibriBold, 9, TextAlign.Left, RgbColor.Black));
                pageElements.Add(new Line(left + 35f, top, left + 135f, top, 0.5f, RgbColor.Black));
                pageElements.Add(new Label(GetOutlookAssessment(outlook), left + 35, top + 4, 50f, 10f, Fonts.CalibriRegular, 9, TextAlign.Left, RgbColor.Black));
            }

            if (type == 1)
            {
                pageElements.Add(new Label("Outlook", left + 35f, top - 10f, 50f, 10f, Fonts.CalibriBold, 9, TextAlign.Left, RgbColor.Black));
                pageElements.Add(new Label(GetOutlookAssessment(outlook), left + 35f, top + 2f, 50f, 10f, Fonts.CalibriRegular, 9, TextAlign.Left, RgbColor.Black));
            }

            if (type == 2)
            {
                pageElements.Add(new Label("Outlook", left + 40f, top + 4f, 50f, 10f, Fonts.CalibriBold, 9, TextAlign.Left, RgbColor.Black));
                pageElements.Add(new Label(GetOutlookAssessment(outlook), left + 40f, top + 16f, 50f, 10f, Fonts.CalibriRegular, 9, TextAlign.Left, RgbColor.Black));
            }
        }

        private static string GetOutlookAssessment(int outlook)
        {
            switch (outlook)
            {
                case -1:
                    return "Negative";
                case 0:
                    return "Neutral";
                case 1:
                    return "Positive";
                default:
                    return string.Empty;
            }
        }


    }
}
